// 云函数入口文件
const cloud = require('wx-server-sdk');
const _ = require('lodash');

cloud.init();
const db = cloud.database();

const CategoryMap = {
  1: { name: '可回收物', tag: 'recyclable' },
  2: { name: '有害垃圾', tag: 'hazardous' },
  4: { name: '厨余垃圾', tag: 'food' },
  8: { name: '其他垃圾', tag: 'residual' },
  16: { name: '可回收物', tag: 'recyclable' },
};
// 云函数入口函数
exports.main = async (event, context) => {
  const { keyword } = event;
  const wxContext = cloud.getWXContext();
  const result = await db.collection('garbage').where({
    name: db.RegExp({ regexp: keyword, options: 'i' })
  })
  .limit(20)
  .get();
  _.forEach(result.data, item => {
    const category = CategoryMap[item.category] || { name: '其他垃圾', tag: 'residual' };
    item.category = category;
  });
  return result.data;
}